package com.csun.cmny.provider.controller.app;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.OldmanAppConstant;
import com.csun.cmny.provider.model.domain.DmcAppSleepDataRecord;
import com.csun.cmny.provider.model.domain.DmcAppSleepRangeRecord;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcDeviceUserBind;
import com.csun.cmny.provider.model.dto.app.AppSleepDateQueryDto;
import com.csun.cmny.provider.model.dto.app.DmcPersonMainDto;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.app.*;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.validator.AppSleepDateQueryValidator;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenzheng
 * @description:家属版老人APP设备相关操作控制器
 * @date 2019/9/25 15:50
 */
@RestController
@RequestMapping(value = "/personMain", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcPersonMainController extends BaseController {
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceUserBindService dmcDeviceUserBindService;

    @Resource
    private DmcCsunSleepDataActionService dmcCsunSleepDataActionService;

    @Resource
    private DmcAppSleepRangeRecordService dmcAppSleepRangeRecordService;

    @Resource
    private DmcAppSleepDataRecordService dmcAppSleepDataRecordService;

    /**
     * 用户添加设备
     * @param personMainDto
     * @return
     */
    @RequestMapping(value = "/userBindsDevice", method = RequestMethod.POST)
    public Wrapper userBindsDevice(@RequestBody DmcPersonMainDto personMainDto) {
        //验证是否传了设备号
        Preconditions.checkArgument(!StringUtils.isEmpty(personMainDto.getDeviceNo()),
                cmnyMessageSource.getMessage("person.device.number.is.null", null));
        personMainDto.setUserId(getLoginAuthDto().getUserId());
        Integer result = dmcDeviceUserBindService.userBindsDevice(personMainDto);
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * 老人绑定设备
     * @param personMainDto
     * @return
     */
    @RequestMapping(value = "/oldmanBindsDevice", method = RequestMethod.POST)
    public Wrapper oldmanBindsDevice(@RequestBody DmcPersonMainDto personMainDto) {
        //参数校验
        if(null == personMainDto.getDeviceId() || personMainDto.getDeviceId() <= 0){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource
                    .getMessage("device.id.null", null));
        }
        if(null == personMainDto.getOldmanId() || personMainDto.getOldmanId() <= 0){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource
                    .getMessage("oldman.id.null", null));
        }
        personMainDto.setUserId(getLoginAuthDto().getUserId());
        Integer result = dmcDeviceUserBindService.saveDeviceOldBind(personMainDto,getLoginAuthDto());
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * 老人解绑设备
     * @param personMainDto
     * @return
     */
    @RequestMapping(value = "/removeOldManDeviceBind", method = RequestMethod.POST)
    public Wrapper removeBind(@RequestBody DmcPersonMainDto personMainDto) {
        //参数校验
        if(null == personMainDto.getDeviceId() || personMainDto.getDeviceId() <= 0){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource
                    .getMessage("device.id.null", null));
        }
        if(null == personMainDto.getOldmanId() || personMainDto.getOldmanId() <= 0){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource
                    .getMessage("oldman.id.null", null));
        }
        personMainDto.setUserId(getLoginAuthDto().getUserId());
        Integer result = dmcDeviceUserBindService.removeOldManBind(personMainDto);
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    /**
     * 用户删除设备
     * @param personMainDto
     * @return
     */
    @RequestMapping(value = "/removeDeviceUserBind", method = RequestMethod.POST)
    public Wrapper removeDeviceUserBind(@RequestBody DmcPersonMainDto personMainDto) {
        //参数校验
        if(null == personMainDto.getDeviceId() || personMainDto.getDeviceId() <= 0){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource
                    .getMessage("device.id.null", null));
        }
        personMainDto.setUserId(getLoginAuthDto().getUserId());
        Integer result = dmcDeviceUserBindService.removeDeviceUserBind(personMainDto);
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                result);
    }

    @RequestMapping(value = "/getSleepDataByDeviceId", method = RequestMethod.POST)
    public Wrapper getSleepDataByDeviceId(@RequestBody AppSleepDateQueryDto appSleepDateQueryDto) {
        AppSingleDaySleepDataVo appSingleDaySleepData = dmcCsunSleepDataActionService.getAppSingleDaySleepData(appSleepDateQueryDto);
        return WrapMapper.ok(appSingleDaySleepData);
    }

    /**
     * 计算睡眠状况数据
     * @param: [appSleepDateQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/getSleepRangesByDeviceIdAndDate", method = RequestMethod.POST)
    public Wrapper getSleepRangesByDeviceIdAndDate(@RequestBody AppSleepDateQueryDto appSleepDateQueryDto) {

        List<DmcAppSleepRangeRecord> dmcAppSleepRangeRecords = dmcAppSleepRangeRecordService.getSleepRangesByDeviceIdAndDate(appSleepDateQueryDto);
//        return WrapMapper.ok(dmcAppSleepRangeRecords);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                dmcAppSleepRangeRecords);
    }


    /**
     * 查询睡眠得分数据
     * @param: [appSleepDateQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/getSleepDataByDeviceIdAndDate", method = RequestMethod.POST)
    public Wrapper getSleepDataByDeviceIdAndDate(@RequestBody AppSleepDateQueryDto appSleepDateQueryDto) {

        AppCountSleepDataVo appCountSleepDataVo = dmcAppSleepDataRecordService.getSleepDataByDeviceIdAndDate(appSleepDateQueryDto);
//        return WrapMapper.ok(appCountSleepDataVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                appCountSleepDataVo);
    }

    /**
     * 设置阈值
     * @param: [dmcPersonMainDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/setDeviceThresholdByDeviceId", method = RequestMethod.POST)
    public Wrapper setDeviceThresholdByDeviceId(@RequestBody DmcPersonMainDto dmcPersonMainDto) {

        int ret = dmcDeviceUserBindService.setDeviceThresholdByDeviceId(dmcPersonMainDto,getLoginAuthDto());
        return WrapMapper.ok(ret);
    }

    /**
     * 阈值查询
     * @param: [dmcPersonMainDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/getDeviceThresholdByDeviceId", method = RequestMethod.POST)
    public Wrapper getDeviceThresholdByDeviceId(@RequestBody DmcPersonMainDto dmcPersonMainDto) {

        DmcDeviceUserBind ret = dmcDeviceUserBindService.getDeviceThresholdByDeviceId(dmcPersonMainDto,getLoginAuthDto());
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 获取报警统计
     * @param: [appSleepDateQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/getAlertCount", method = RequestMethod.POST)
    public Wrapper getAlertCount(@RequestBody AppSleepDateQueryDto appSleepDateQueryDto) {
        AppAlertCountVo vo = dmcAppSleepDataRecordService.getAlertCount(appSleepDateQueryDto,getLoginAuthDto());
//        return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

    /**
     * 老人睡眠比例统计
     * @param: [appSleepDateQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/getOldmanSleepTimePercent", method = RequestMethod.POST)
    public Wrapper getOldmanSleepTimePercent() {
        AppSleepRateCountVo vo = new AppSleepRateCountVo();
        vo.setOldmanSleepTimeCount(OldmanAppConstant.OLDMAN_SLEEP_TIME_CONSTANT);
        vo.setOldmanSleepDurationCount(OldmanAppConstant.OLDMAN_SLEEP_DURATION_CONSTANT);
//        return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

    /**
     * 查询本周期和上周期睡眠总时长和平均得分
     * @param: [appSleepDateQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/getThisCycleDataAndLastCycleData", method = RequestMethod.POST)
    public Wrapper getThisCycleDataAndLastCycleData(@Validated(value = {AppSleepDateQueryValidator.class}) @RequestBody AppSleepDateQueryDto appSleepDateQueryDto, BindingResult br) {
        AppDataCompareVo vo = dmcAppSleepDataRecordService.getThisCycleDataAndLastCycleData(appSleepDateQueryDto, getLoginAuthDto());
//        return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

    /**
     * 手动生成睡眠数据
     * @param: [appSleepDateQueryDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/saveAppSingleDaySleepData", method = RequestMethod.POST)
    public Wrapper saveAppSingleDaySleepData(@Validated(value = {AppSleepDateQueryValidator.class}) @RequestBody AppSleepDateQueryDto appSleepDateQueryDto, BindingResult br) {
        int ret = dmcAppSleepDataRecordService.saveAppSingleDaySleepData(appSleepDateQueryDto.getDeviceId(), appSleepDateQueryDto.getDate());
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }
}
